// pages/Certification/Certification.js
const app = getApp()
const util = require('../../../utils/util.js')
const http = require('../../../utils/http.js');
Page({
  data: {
    avatarUrl1: 'http://oss.myzy.com.cn/wechat/images/imag_shenfenzheng3.png',
    avatarUrl2: 'http://oss.myzy.com.cn/wechat/images/imag_shenfenzheng4.png',
    actionSheetItems: ['拍摄', '相册'],
    base641: '',
    base642: '',
    actionSheetHidden: true,
    hide: true,
    hide1: true,
    hide2: true,
  },
  listenerButton: function (e) {
    let id = e.currentTarget.dataset.id;
    this.setData({
      //取反
      actionSheetHidden: !this.data.actionSheetHidden,
      id: id
    });
  },
  listenerActionSheet: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  itemChange(e) {
    var text = e.target.dataset.text;
    var that = this;
    var id = this.data.id;
    that.setData({
      actionSheetHidden: true
    })
    if (text == '拍摄') {
      this.chooseCrame(id);
    } else {
      this.chooseInt(id);
    }
  },
  chooseCrame(id) {
    var id = this.data.id;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: res => {
        //设置头像  本地路径
        let tempFilePaths = res.tempFilePaths[0];
        if (id == 1) {
          this.ocrImage('front', tempFilePaths, 'front', id, tempFilePaths)
        } else {
          this.ocrImage('back', tempFilePaths, 'back', id, tempFilePaths)
        }
      }
    })
  },
  chooseInt(id) {
    var id = this.data.id;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
      success: res => {
        //设置头像  本地路径
        let tempFilePaths = res.tempFilePaths[0];
        if (id == 1) {
          this.ocrImage('front', tempFilePaths, 'front', id, tempFilePaths)
        } else {
          this.ocrImage('back', tempFilePaths, 'back', id, tempFilePaths)
        }
      }
    })
  },
  onLoad: function (options) {
    if (options.agent) {
      this.setData({
        agent: options.agent
      })
    }
  },
  onReady: function () {
    this.toastDialog = this.selectComponent("#toastDialog"); //此处id和页面的 wxml中的id一致
    this.toasts = this.selectComponent('#toasts')
  },
  onShow: function () {},
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  // 图片识别
  ocrImage(back, file, slide, id, tempFilePaths) {
    wx.showToast({
      title: '上传中',
      icon: 'loading'
    })
    let datas = {
      [back]: file,
      side: slide
    }
    console.log(datas)
    let data = {
      side: slide
    }
    const header = http.getHeader(datas, 'v1/orcImages')
    header.debug = true
    wx.uploadFile({
      url: app.globalData.baseUrl + 'v1/orcImages',
      method: 'post',
      header: header,
      name: back,
      filePath: tempFilePaths,
      formData: datas,
      success: res => {
        var res = res.data;
        res = JSON.parse(res);
        console.log('测试数据=======', res)
        if (res.code == 200) {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
          // this.toastDialog.showDialog(res.msg)
          if (id == 1) {
            this.setData({
              hide1: false,
              avatarUrl1: tempFilePaths
            });
          } else {
            this.setData({
              hide2: false,
              avatarUrl2: tempFilePaths
            });
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
          // this.toastDialog.showDialog(res.msg)
        }
      }
    })
  },
  //实名认证提交
  realname(data) {
    var _this = this
    http.postRequest('v1/realname', data,
      function (res) {
        wx.setStorageSync('renzheng', 1)
        wx.setStorageSync('shiming', res.data)
        wx.showToast({
          title: res.msg,
          icon: "success"
        })
        _this.syncData({is_sync:1})
        // _this.toastDialog.showDialog(res.msg)
        setTimeout(() => {
          wx.redirectTo({
            url: '/pages/onNewPages/CerSuc/CerSuc?data=' + JSON.stringify(res.data),
          })
        }, 1500);
 
      },
      function (err) {
        wx.showToast({
          title: err.msg,
          icon: "none"
        })
        // _this.toastDialog.showDialog(err.msg)
      })
  },
  //缓存数据
  syncData(data) {
    var _this = this
    http.postRequest('syncData', data,
      function (res) {
        wx.setStorageSync('member', res.data)
        getApp().globalData.token = res.data.token;
        if (res.code == 200) {
          console.log(res)
          if (res.data.member_info != undefined) {
            wx.setStorageSync('renzheng', 1)
          }
        }
      },
      function (res) {})
  },
  //实名认证
  Sure() {
    this.realname('');
  },
})